import java.util.Stack;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 09 - 30
 * Time: 12:47
 */
//牛客 合法括号序列判断
public class Solution1 {
    public boolean chkParenthesis(String A, int n) {
        if(n % 2 != 0) return false;
        Stack<Character> stack = new Stack<>();
        for (char ch : A.toCharArray()) {
            if(ch == '(') {
                stack.push(ch);
            }else if(ch == ')'){
                if(stack.isEmpty()) {
                    return false;
                }else if(stack.peek() == '(') {
                    stack.pop();
                }
            }else {
                return false;
            }
        }
        return stack.isEmpty();
    }
}
